<template>
  <div class="user-edit-container">
    <!-- Header 区域 -->
    <van-nav-bar
      title="编辑资料"
      left-arrow
      @click-left="$router.back()"
      fixed
    />

    <!-- 用户资料 -->
    <van-cell-group class="action-card">
      <van-cell title="头像" is-link center>
        <template #default>
          <van-image round class="avatar" :src="imgUrl" @click="imgClickFn" />
          <input
            type="file"
            ref="iptFile"
            v-show="false"
            accept="image/*"
            @change="onFileChange"
          />
        </template>
      </van-cell>
      <van-cell
        title="名称"
        is-link
        :value="profileObj.name"
        @click="nameClickFn"
      />
      <van-cell
        title="生日"
        is-link
        :value="profileObj.birthday"
        @click="birClickFn"
      />
    </van-cell-group>
    <van-dialog
      v-model="show"
      title="标题"
      show-cancel-button
      :before-close="beforeCloseFn"
    >
      <input type="text" v-fofo v-model="inputUserName" />
    </van-dialog>
    <van-popup v-model="dateShow" position="bottom" :style="{ height: '50%' }" round>
      <van-datetime-picker
        v-model="currentDate"
        type="date"
        title="选择年月日"
        :min-date="minDate"
        :max-date="maxDate"
        @confirm = "confirmFn"
        @cancel = "cancelFn"
      />
    </van-popup>
  </div>
</template>

<script>
import { formatDate } from '@/utils/date.js'
import {
  userProfileAPI,
  updateUserPhotoAPI,
  updateUserProfileAPI
} from '@/api'
// import { Notify } from 'vant'
import Notify from '@/ui/Notify'
import { mapMutations } from 'vuex'
export default {
  name: 'UserEdit',
  data () {
    return {
      profileObj: {},
      imgUrl: '',
      show: false,
      inputUserName: '',
      dateShow: false,
      minDate: new Date(1920, 0, 1),
      maxDate: new Date(),
      currentDate: new Date()
    }
  },
  async created () {
    const res = await userProfileAPI()
    this.profileObj = res.data.data
    this.imgUrl = this.profileObj.photo
    this.inputUserName = this.profileObj.name
  },
  methods: {
    async onFileChange (e) {
      if (e.target.files.length === 0) {
        return
      }
      const fd = new FormData()
      fd.append('photo', e.target.files[0])
      const res = await updateUserPhotoAPI(fd)
      this.imgUrl = res.data.data.photo
      this.SET_PHOTO(this.imgUrl)
    },
    imgClickFn () {
      this.$refs.iptFile.click()
    },
    nameClickFn () {
      this.show = true
    },
    async beforeCloseFn (action, done) {
      if (action === 'confirm') {
        const reg = /^[A-Za-z0-9\u4e00-\u9fa5]{1,7}$/
        if (reg.test(this.inputUserName)) {
          await updateUserProfileAPI({ name: this.inputUserName })
          this.profileObj.name = this.inputUserName
          done()
        } else {
          Notify({ type: 'warning', message: '用户名只能是1-7位中英数字组合' })
          done(false)
        }
      } else {
        done()
        this.inputUserName = this.profileObj.name
      }
    },
    birClickFn () {
      this.dateShow = true
      this.currentDate = new Date(this.profileObj.birthday)
    },
    async  confirmFn () {
      await updateUserProfileAPI({ birthday: this.currentDate })
      this.dateShow = false
      this.profileObj.birthday = formatDate(this.currentDate)
    },
    cancelFn () {
      this.dateShow = false
    },
    ...mapMutations(['SET_PHOTO'])
  }
}
</script>

<style lang="less" scoped>
.user-edit-container {
  padding-top: 46px;
  .avatar {
    width: 50px;
    height: 50px;
  }
}
::v-deep .van-dialog__content {
  text-align: center;
  input {
    padding: 0;
    outline: none;
    border: none;
    text-align: center;
  }
}
</style>
